package org.dync.giftlibrary.util;

import ohos.agp.components.Component;
import ohos.agp.components.LayoutScatter;
import ohos.agp.components.ListContainer;
import ohos.agp.components.TableLayoutManager;
import ohos.app.Context;
import org.dync.giftlibrary.ResourceTable;
import org.dync.giftlibrary.adapter.FaceGVAdapter;
import org.dync.giftlibrary.widget.GiftModel;

import java.util.ArrayList;
import java.util.List;

public class ExpressionUtil {
    public static String ASSETS_ROOT = "p/";
    public static String ASSETS_GIF_ROOT = "g/";
    private boolean isNetData = false;//是否来自网络数据

    public ExpressionUtil(boolean isNetData) {
        this.isNetData = isNetData;
    }

    /**
     * 横屏时显示
     *
     * @param context
     * @param recyclerView
     * @param staticGiftsList @return
     */
    public void giftView(final Context context, ListContainer recyclerView, List<GiftModel> staticGiftsList) {
        final FaceGVAdapter mGvAdapter = new FaceGVAdapter(recyclerView, staticGiftsList, context, isNetData);
        recyclerView.setItemProvider(mGvAdapter);

        // 单击表情执行的操作
        mGvAdapter.setOnItemClickListener((view, giftModel, position) -> {
            try {
                String giftPic = giftModel.getGiftPic();
                String giftName = giftModel.getGiftName();
                String giftPrice = giftModel.getGiftPrice();
                if (giftClickListener != null) {
                    giftClickListener.onClick(position, giftPic, giftName, giftPrice);
                }
            } catch (Exception e) {
                e.printStackTrace();
            }
        });
    }

    /**
     * 竖屏时显示，每一页的数据
     *
     * @param context
     * @param position 第几页
     * @param staticGiftsList 表情集合
     * @param columns 列数
     * @param rows 行数
     * @param showView View
     * @return
     */
    public Component viewPagerItem(final Context context, int position, List<GiftModel> staticGiftsList, int columns, int rows, final Component showView) {
        Component layout = LayoutScatter.getInstance(context).parse(ResourceTable.Layout_face_gridview, null, false);

        ListContainer recyclerView = (ListContainer) layout.findComponentById(ResourceTable.Id_chart_face_gv);
        TableLayoutManager girdLayoutManager = new TableLayoutManager();
        girdLayoutManager.setColumnCount(6);
        girdLayoutManager.setRowCount(4);
        recyclerView.setLayoutManager(girdLayoutManager);

        List<GiftModel> subList = new ArrayList<>();
        subList.addAll(staticGiftsList
                .subList(position * (columns * rows - 0),
                        (columns * rows - 0) * (position + 1) > staticGiftsList
                                .size() ? staticGiftsList.size() : (columns * rows - 0)
                                * (position + 1)));

        final FaceGVAdapter mGvAdapter = new FaceGVAdapter(recyclerView, subList, context, isNetData);
        recyclerView.setItemProvider(mGvAdapter);
        // 单击表情执行的操作
        mGvAdapter.setOnItemClickListener((view, giftModel, position1) -> {
            try {
                String giftPic = giftModel.getGiftPic();
                String pngStr = giftModel.getGiftName();
                String giftPrice = giftModel.getGiftPrice();
                if (giftClickListener != null) {
                    giftClickListener.onClick(position1, giftPic, pngStr, giftPrice);
                }
            } catch (Exception e) {
                e.printStackTrace();
            }
        });

        return recyclerView;
    }

    public interface GiftClickListener {
        void onClick(int position, String giftPic, String giftName, String giftPrice);
    }

    private GiftClickListener giftClickListener;

    public void setGiftClickListener(GiftClickListener listener) {
        giftClickListener = listener;
    }

    /**
     * 根据表情数量以及GridView设置的行数和列数计算Pager数量
     *
     * @param listSize
     * @param columns
     * @param rows
     * @return int
     */
    public int getPagerCount(int listSize, int columns, int rows) {
        return listSize % (columns * rows - 0) == 0 ? listSize / (columns * rows - 0) : listSize / (columns * rows - 0) + 1;
    }

    /**
     * 初始化表情列表staticGiftsList
     *
     * @return List
     */
    public List<GiftModel> initStaticGifts() {
        List<GiftModel> giftsList = new ArrayList<>();
        for (int i = 0; i < 144; i++) {
            String mediaPath = "";
            GiftModel giftModel = new GiftModel();
            if (i < 10) {
                mediaPath = "entry/resources/base/media/00" + i + ".png";
            } else if (i < 100) {
                mediaPath = "entry/resources/base/media/0" + i + ".png";
            } else if (i < 143) {
                mediaPath = "entry/resources/base/media/" + i + ".png";
            } else if (i == 143) {
                mediaPath = "entry/resources/base/media/del.png";
            }
            System.out.println(mediaPath);
            giftModel.setGiftName(mediaPath);
            giftsList.add(giftModel);
        }
        return giftsList;
    }
}
